#!/bin/bash

# Author leman
# time: 2020-06-24
# blog: https://www.cnblogs.com/lemanlai/

function os_info(){
	if [ -a /etc/redhat-release -a  `cat /etc/redhat-release |egrep -i 'centos|redhat'|grep 7[.0-9]|wc -l` -eq 1 ];
	then
		echo "OS version is good"
	else
		echo "Only support CentOS|redhat 7"
		exit 1
	fi
}

function usage(){
	echo "please run  with  ./k8s_install  [init|harbor|master|worker|help]"
}
function help(){
	echo "./k8s_install options"
	echo "   "
	echo "options: 
			init  : initial os env.
			harbor: install harbor repo for k8s
			master: install master role for k8s
			worker: install worker role for k8s
			help: get help
	"
}

function os(){
#关闭防火墙和selinux
	systemctl stop firewalld 
	systemctl disable firewalld
	setenforce 0
	sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

	#禁用虚拟内存
	swapoff -a
	sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
}
function make_yum(){
#配置清华源
	rm -rf /etc/yum.repos.d/*

	curl -o /etc/yum.repos.d/CentOS-Base.repo https://files-cdn.cnblogs.com/files/lemanlai/CentOS-7.repo.sh

	curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-7 https://mirror.tuna.tsinghua.edu.cn/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7

	yum install epel-release -y
	yum clean all
	yum makecache fast
	yum install nmap telnet curl wget vim lrzsz bind-utils -y #安装基本工具
}

function Docker-Ce(){
#docker ce 安装
	yum remove docker docker-common docker-selinux docker-engine -y  #如果你之前安装过 docker，请先删掉
	yum install -y yum-utils device-mapper-persistent-data lvm2 #安装一些依赖
	curl -o /etc/yum.repos.d/docker-ce.repo https://files-cdn.cnblogs.com/files/lemanlai/docker-ce.repo.sh
	yum clean all
	yum makecache fast
	yum -y install docker-ce
	systemctl enable docker
	curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io  #写入加速地址
	systemctl daemon-reload
	systemctl restart docker #重启服务
}

function make_docker-compose(){
	curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
	chmod +x /usr/local/bin/docker-compose
}

function harbor(){
	cd /opt/
	wget https://leman-odd.oss-cn-shenzhen.aliyuncs.com/k8s/harbor-offline-installer-v1.3.0-rc4.tgz
	tar -xvf harbor-offline-installer-v1.3.0-rc4.tgz
	cd  harbor/
	sed -i.bak 's\reg.mydomain.com\harbor\g' harbor.cfg
	./install.sh
	echo "----Harbor has been installed and started successfully "
	echo " please login by https://your_ip"
	echo "login account:  admin     password: Harbor12345 "

}

function harbor_install(){
	if [ `id -u` -eq 0 ];
	then
		os
		make_yum
		Docker-Ce
		make_docker-compose
		harbor
	else 
		echo "please switch to user root first"
		exit 1
	fi
}

function init(){
	os
	make_yum
}
function main(){
	os_info
	case $1 in
		"init")
			init;;
		"harbor")
			harbor_install;;
		"master")
			echo "master";;
		"worker")
			echo "worker";;
		"help")
			help;;
			*) 
			usage;;
	esac
}

main
